﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.Admin.DicClassManage.Controls
{
	public partial class AddDicClass : BaseUserControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			this.DataBind();
		}

		/// <summary>
		/// 属性 ClassID
		/// </summary>
		public int ClassID
		{
			get
			{
				return Convert.ToInt32(CtrlClassID.Text);
			}
			set
			{
				CtrlClassID.Text = value.ToString();
			}
		}

		/// <summary>
		/// 属性 ClassName
		/// </summary>
		public string ClassName
		{
			get
			{
				return Convert.ToString(CtrlClassName.Text);
			}
			set
			{
				CtrlClassName.Text = value.ToString();
			}
		}

		/// <summary>
		/// 属性 ClassCode
		/// </summary>
		public string ClassCode
		{
			get
			{
				return Convert.ToString(CtrlClassCode.Text);
			}
			set
			{
				CtrlClassCode.Text = value.ToString();
			}
		}

		/// <summary>
		/// 属性 Memo
		/// </summary>
		public string Memo
		{
			get
			{
				if (String.IsNullOrEmpty(CtrlMemo.Text))
					return null;

				return Convert.ToString(CtrlMemo.Text);
			}
			set
			{
				if (value == null)
					CtrlMemo.Text = "";
				else
					CtrlMemo.Text = value.ToString();
			}
		}


		/// <summary>
		/// 控件的 DicClassInfo 实例。
		/// </summary>
		public DicClass Info
		{
			get
			{
				DicClass dicClass = new DicClass();

				dicClass.ClassId = ClassID;
				dicClass.ClassName = ClassName;
				dicClass.ClassCode = ClassCode;
				dicClass.Memo = Memo;

				return dicClass;
			}
			set
			{
				if (value == null) return;

				ClassID = value.ClassId;
				ClassName = value.ClassName;
				ClassCode = value.ClassCode;
				Memo = value.Memo;
			}
		}

		#region 验证
		protected void btnValidateClassID_Click(object sender, EventArgs e)
		{
			Page.Validate();
			if (RFVCtrlClassID.IsValid && REVCtrlClassID.IsValid && CVCtrlClassID.IsValid)
				lValidateClassID.Visible = true;
			else
				lValidateClassID.Visible = false;
		}

		protected void btnValidateClassCode_Click(object sender, EventArgs e)
		{
			Page.Validate();
			if (RFVCtrlClassCode.IsValid && CVCtrlClassCode.IsValid)
				lValidateClassCode.Visible = true;
			else
				lValidateClassCode.Visible = false;
		}

		protected void CVCtrlClassID_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
		{
			args.IsValid = false;
			int identifier;
			if (Int32.TryParse(args.Value, out identifier))
				args.IsValid = !DicClasses.Exists(identifier);
		}

		protected void CVCtrlClassCode_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
		{
			args.IsValid = !DicClasses.Exists(SiteId, args.Value);
		}
		#endregion
	}
}